<template>
  <div class="loginlist">
    <van-nav-bar title="登录" class="nav-tou" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        name="mobile"
        label="手机号"
        placeholder="手机号"
        required
        :rules="[
          { required: true, message: '请填写手机号' },
          {
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '格式错误'
          }
        ]"
      />
      <van-field
        v-model="code"
        name="code"
        label="验证码"
        placeholder="验证码"
        required
        :rules="[
          { required: true, message: '请填写验证码' },
          { pattern: /\d{6}/, message: '格式错误' }
        ]"
      />
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :loading="isloding"
          :disabled="isloding"
          loading-text="加载中"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import { loginjie } from '../../api/login'
import { settoken } from '../../utils/token'
export default {
  name: 'login',
  data () {
    return {
      mobile: '13912345678',
      code: '246810',
      isloding: false
    }
  },
  methods: {
    onSubmit (values) {
      this.isloding = true
      loginjie(values)
        .then(res => {
          settoken(res.data.data)
          setTimeout(() => {
            this.isloding = false
            this.$toast.success('成功')
            const redirect = this.$route.query.redirect
            if (redirect) {
              return this.$router.push({ path: redirect })
            }
            this.$router.push({ path: '/home' })
          }, 500)
        })
        .catch(err => {
          setTimeout(() => {
            this.isloding = false
            this.$toast.fail('失败')
            console.log(err)
          }, 500)
        })
    }
  }
}
</script>

<style lang="less">
.loginlist {
  .nav-tou {
    background-color: #3295fb;
    .van-ellipsis {
      color: #fff;
    }
  }
}
</style>
